package org.aaron.demos;

import android.app.Activity;
import android.hardware.Sensor;
import android.hardware.SensorEvent;
import android.hardware.SensorEventListener;
import android.hardware.SensorManager;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.text.method.ScrollingMovementMethod;
import android.util.Log;
import android.view.View;
import android.widget.Button;
import android.widget.TextView;

public class SensorManagerActivity extends Activity implements SensorEventListener {

	private SensorManager mSensorManager;
	private Sensor sensor;
	private Button lightbtn, accelerometerbtn, rangebtn, orientationbtn;
	private TextView textView1;
	private float gravity[] = new float[3];
	private float linear_acceleration[] = new float[3];

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.sensormanagerlayout);
		mSensorManager = (SensorManager) getSystemService(SENSOR_SERVICE);
		lightbtn = (Button) findViewById(R.id.lightbtn);
		accelerometerbtn = (Button) findViewById(R.id.accelerometerbtn);
		rangebtn = (Button) findViewById(R.id.rangebtn);
		orientationbtn = (Button) findViewById(R.id.orientationbtn);
		View.OnClickListener listener = new ButtonClickListener();
		lightbtn.setOnClickListener(listener);
		accelerometerbtn.setOnClickListener(listener);
		rangebtn.setOnClickListener(listener);
		orientationbtn.setOnClickListener(listener);
		textView1 = (TextView) findViewById(R.id.textView1);
		textView1.setMovementMethod(new ScrollingMovementMethod());
	}

	private Handler handler = new Handler() {
		@Override
		public void handleMessage(Message msg) {
			super.handleMessage(msg);
			Bundle data = msg.getData();
			textView1.append("\n" + "data x:" + data.getFloat("x") + "data y:" + data.getFloat("y") + "data z:" + data.getFloat("z"));
		}
	};

	protected void onPause() {
		super.onPause();
		mSensorManager.unregisterListener(this);
	};

	@Override
	public void onAccuracyChanged(Sensor sensor, int accuracy) {

	}

	@Override
	public void onSensorChanged(SensorEvent event) {
		// 在本例中，alpha 由 t / (t + dT)计算得来，
		// 其中 t 是低通滤波器的时间常数，dT 是事件报送频率

		if (Sensor.TYPE_ACCELEROMETER == event.sensor.getType()) {
			final float alpha = 0.8F;

			// 用低通滤波器分离出重力加速度
			gravity[0] = alpha * gravity[0] + (1 - alpha) * event.values[0];
			gravity[1] = alpha * gravity[1] + (1 - alpha) * event.values[1];
			gravity[2] = alpha * gravity[2] + (1 - alpha) * event.values[2];

			// 用高通滤波器剔除重力干扰
			linear_acceleration[0] = event.values[0] - gravity[0];
			linear_acceleration[1] = event.values[1] - gravity[1];
			linear_acceleration[2] = event.values[2] - gravity[2];
		} else {
			linear_acceleration[0] = event.values[0];
			linear_acceleration[1] = event.values[1];
			linear_acceleration[2] = event.values[2];
		}
		Bundle data = new Bundle();
		data.putFloat("x", linear_acceleration[0]);
		data.putFloat("y", linear_acceleration[1]);
		data.putFloat("z", linear_acceleration[2]);
		Message message = handler.obtainMessage();
		message.setData(data);
		handler.sendMessage(message);
	}

	private class ButtonClickListener implements View.OnClickListener {

		@Override
		public void onClick(View v) {
			textView1.setText("");
			switch (v.getId()) {
			case R.id.lightbtn:
				sensor = mSensorManager.getDefaultSensor(Sensor.TYPE_LIGHT);
				mSensorManager.registerListener(SensorManagerActivity.this, sensor, SensorManager.SENSOR_DELAY_NORMAL);
				break;
			case R.id.accelerometerbtn:
				sensor = mSensorManager.getDefaultSensor(Sensor.TYPE_ACCELEROMETER);
				mSensorManager.registerListener(SensorManagerActivity.this, sensor, SensorManager.SENSOR_DELAY_NORMAL);
				break;
			case R.id.rangebtn:
				sensor = mSensorManager.getDefaultSensor(Sensor.TYPE_PROXIMITY);
				mSensorManager.registerListener(SensorManagerActivity.this, sensor, SensorManager.SENSOR_DELAY_NORMAL);
				break;
			case R.id.orientationbtn:
				sensor = mSensorManager.getDefaultSensor(Sensor.TYPE_ORIENTATION);
				mSensorManager.registerListener(SensorManagerActivity.this, sensor, SensorManager.SENSOR_DELAY_NORMAL);
				break;
			default:
				break;

			}
		}
	}
}
